<template>
  <div class="cartcontrol">
    <transition name="move">
      <div class="cart-subtract" @click.stop.prevent="subtractCount" v-if="food.count > 0">
        <span class="icon-remove_circle_outline inner"></span>
      </div>
    </transition>
    <div class="cart-count" v-if="food.count > 0">{{food.count}}</div>
    <div class="cart-add icon-add_circle" @click.stop.prevent="addCount"></div>
  </div>
</template>

<script>
import Vue from 'vue'
export default {
  props: ['food'],
  data() {
    return {}
  },
  created() {
    console.log('加载')
  },
  methods: {
    addCount(event) {
      // 当给一个观测对象添加一个不存在的属性的时候直接赋值是不可以的，definProprty检测不到 所以不是响应式
      // 需要使用Vue.set进行添加
      console.log(event.target)
      if (!event._constructed) {
        return false
      }
      if (!this.food.count) {
        Vue.set(this.food, 'count', 1)
      } else {
        this.food.count++
      }
      this.$emit('drop', event.target)
      this.$emit('setSelectFoods')
    },
    subtractCount() {
      this.food.count--
      if (this.food.count < 0) {
        this.food.count = 0
      }
      this.$emit('setSelectFoods')
    }
  }
}
</script>

<style lang="stylus" scoped>
.cartcontrol {
  font-size: 0;
  position: relative;

  .cart-subtract, .cart-count, .cart-add {
    vertical-align: middle;
    display: inline-block;
  }

  .cart-count {
    font-size: 0.2rem;
    min-width: 0.24rem;
    text-align: center;
  }

  .cart-subtract .inner, .cart-add {
    font-size: 0.48rem;
    line-height: 0.48rem;
    padding: 0.12rem;
    color: rgb(0, 160, 220);
  }

  .cart-subtract {
    transition: all 0.4s linear;

    .inner {
      display: inline-block;
      transition: all 0.4s linear;
    }

    &.move-transition {
      opacity: 1;
      transform: translate3d(0, 0, 0);

      .inner {
        transform: rotate(0);
      }
    }

    &.move-enter, &.move-leave-to {
      opacity: 0;
      transform: translate3d(0.48rem, 0, 0);

      .inner {
        transform: rotate(180deg);
      }
    }
  }
}
</style>
